package com.company;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入规律pattern:");
        String pattern = sc.nextLine();
        System.out.println("请输入字符串:");
        String str = sc.nextLine();
        if(wordPattern(pattern,str)){
            System.out.println("字符串\"" + str + "\"遵循规律\"" + pattern + "\"");
        } else {
            System.out.println("字符串\"" + str + "\"不遵循规律\"" + pattern + "\"");
        }

    }

    public static boolean wordPattern(String pattern, String s) {
        HashMap<Character,String> map = new HashMap<>();
        String[] buffer = s.split(" ");
        if (buffer.length != pattern.length()) {
            return false;
        }
        for(int i = 0; i < pattern.length(); i++){
            char ch = pattern.charAt(i);
            if(!map.containsKey(ch)){
                if(!map.containsValue(buffer[i])){
                    map.put(ch,buffer[i]);
                }else {
                    return false;
                }
            }else if(!map.get(ch).equals(buffer[i])){
                return false;
            }
        }
        return true;
    }
}
